Eureka+Spring Cloud

Eureka 搭建

使用eureka作为分布式服务注册中心

Eureka pom引入

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <!--jjwt-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>false</overwrite>
                    <configurationFile>
                        generator_base.xml
                    </configurationFile>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>

Eureka(两个注册中心) application.properties 配置

eureka-1

server.port=9000
#开启安全认证
spring.security.user.name=myeureka
spring.security.user.password=123456
#是否向服务注册中心注册自己
eureka.client.register-with-eureka=false
#是否检索服务
eureka.client.fetch-registry=false
#指定服务注册中心的位置
eureka.client.service-url.defaultZone=http://127.0.0.1:9000
#逐下线的服务,间隔,5秒,默认是60
eureka.server.evictionIntervalTimerInMs=5000
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

eureka-2

server.port=10000
#开启安全认证
spring.security.user.name=myeureka-b
spring.security.user.password=123456
#是否向服务注册中心注册自己
eureka.client.register-with-eureka=false
#是否检索服务
eureka.client.fetch-registry=false
#指定服务注册中心的位置
eureka.client.service-url.defaultZone=http://127.0.0.1:10000
#逐下线的服务,间隔,5秒,默认是60
eureka.server.evictionIntervalTimerInMs=5000
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

Eureka Application添加注解

@SpringBootApplication
@EnableEurekaServer
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})

Eureka集群后,服务调用失败解决

在Eureka项目添加配置项,关闭csrf

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable(); //关闭csrf
        super.configure(http);
    }
}

Spring Cloud 搭建

建立一个client,一个serve,简单模拟feign调用

Spring Cloud pom引入

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Spring Cloud application.properties配置

由于Eureka设置了安全登录密码,所以添加注册中心地址时需要加入账号和密码

client-Apple配置

server.port=9005
spring.application.name=eureka-apple
eureka.client.service-url.defaultZone=http://myeureka:123456@localhost:9000/eureka,http://myeureka-b:123456@localhost:10000/eureka
#心跳间隔时间,默认是30秒
eureka.instance.leaseRenewalIntervalInSeconds=2
#最后一次心跳时间后leaseExpirationDurationInSeconds秒就认为是下线了,默认是90秒
eureka.instance.leaseExpirationDurationInSeconds=5
#默认feign的hystrix为关闭状态
feign.hystrix.enabled=true
#设置熔断超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
#ribbon.http.client.enabled=true
#ribbon负载均衡规则-随机
eureka-orange.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
#请求连接超时时间
eureka-orange.ribbon.ConnectTimeout=3000
#请求处理的超时时间
eureka-orange.ribbon.ReadTimeout=4000
#对所有请求都进行重试
eureka-orange.ribbon.OkToRetryOnAllOperations=true
#切换实例的重试次数
eureka-orange.ribbon.MaxAutoRetriesNextServer=2
#对当前实例的重试次数
eureka-orange.ribbon.MaxAutoRetries=1

server-Orange配置

server.port=9006
spring.application.name=eureka-orange
eureka.client.service-url.defaultZone=http://myeureka:123456@localhost:9000/eureka/,http://myeureka-b:123456@localhost:10000/eureka/
#心跳间隔时间,默认是30秒
eureka.instance.leaseRenewalIntervalInSeconds=2
#最后一次心跳时间后leaseExpirationDurationInSeconds秒就认为是下线了,默认是90秒
eureka.instance.leaseExpirationDurationInSeconds=5

server Orange

提供服务

启动类添加注解
@SpringBootApplication
@EnableEurekaClient
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

controller

@RestController
public class TestController {
    @RequestMapping(value = "/hello")
    public String hello() {
        System.out.println("===开始处理===" + new Date());
        ///等待4秒
        // Thread.sleep(4001);
        System.out.println("===处理结束===" + new Date());
        return "hello!-orange-1";
    }
}

client Apple

调用Orange

启动类添加注解
@SpringBootApplication
@EnableEurekaClient
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

feign,name填写注册中心要调用的服务名

@FeignClient(name = "eureka-orange",fallback = FeignClientFallback.class)
public interface AppleFeign {
    /**
     * feign远程调用-hello
     * @return
     */
    @RequestMapping(value = "/hello")
    String hello();
}

失败回调,实现AppleFeign

@Component
public class FeignClientFallback implements AppleFeign{

    @Override
    public String hello() {
        return "熔断回调!";
    }
}

controller调用

@Controller
public class TestController {
    @Autowired
    private AppleFeign appleFeign;

    @RequestMapping(value = "/hello")
    @ResponseBody
    public String hello() {
        return "hello!-apple";
    }

    @RequestMapping(value = "/use")
    @ResponseBody
    public String use() {
        String s = appleFeign.hello();
        return s;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值