在实践过程中遇到哪些问题?
1、当provider加上server.context-path,Feign客户端该如何使用?
2、如何解决上述问题导致的监控检查url不对问题?
3、常见的配置信息有哪些?
如果您也有这些疑惑请往下看;
代码实践
一、各服务都加入Spring Cloud 依赖:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-build-dependencies</artifactId>
<version>1.3.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- provider、consume 客户端引入-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- eureka-server引入 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
</dependencies>
二、编写注册中心 eureka-register:
application.properties配置:
server.port=8301
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
#是否开启自我保护
eureka.server.enable-self-preservation=false
#清理间隔(单位毫秒,默认是60*1000)
eureka.server.eviction-interval-timer-in-ms=5000
#eureka.instance.ipAddress=192.168.10.55
eureka.instance.preferIpAddress=true
#心跳间隔
eureka.instance.lease-renewal-interval-in-seconds=5
#即服务续约到期时间(缺省为90s)
eureka.instance.lease-expiration-duration-in-seconds=10
编写服务类:
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
private static final Logger logger = LoggerFactory.getLogger(EurekaApplication.class);
public static void main(String[] args) {
System.setProperty("spring.config.location", "classpath:conf/env/application.properties");
SpringApplication.run(EurekaApplication.class, args);
logger.info("start completed !");
}
}
三、编写服务提供者 rpc-demo-provider:
application.properties配置:
spring.application.name=rpc-demo-provider
server.context-path=/provider
server.port=8083
eureka.client.serviceUrl.defaultZone=http://192.168.10.55:8301/eureka/
#优先显示IP做为主机名
eureka.instance.prefer-ip-address=true
#eureka client间隔多久去拉取服务注册信息,默认为30秒
eureka.client.registry-fetch-interval-seconds=10
#心跳间隔
eureka.instance.lease-renewal-interval-in-seconds=5
#即服务续约到期时间(缺省为90s)
eureka.lease-expiration-duration-in-seconds=10
#由于加了context-path所以得修改下instance状态地址,便于eureka server、spring admin等健康检查
eureka.instance.statusPageUrlPath=${server.context-path}/info
eureka.instance.healthCheckUrlPath=${server.context-path}/health
eureka.instance.homePageUrl=${server.context-path}
eureka.instance.context-path=${server.context-path}
instance.metadata-map.management.context-path=${server.context-path}/actuator
#spring boot一般日志配置
logging.file=rpc-demo-provider.log
logging.level.root=info
logging.level.com.huize.framework=DEBUG
logging.pattern.file=%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
logging.pattern.console=%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
logging.file.max-size=100M
logging.file.max-history=2
编写服务类:
@RestController
@RequestMapping("/user")
public class UserController{
@RequestMapping("/sayHello")
@Override
public String sayHello() {
return "hello feign";
}
@RequestMapping("/getUser")
@Override
public User getUser() {
User user = new User();
user.setAge(25);
user.setBrithday(new Date());
user.setName("张三");
return user;
}
}
@EnableEurekaClient
@SpringBootApplication
public class ProviderApplication {
private final static Logger logger = LogManager.getLogger(ProviderApplication.class);
public static void main(String[] args) {
//修改application location
System.setProperty("spring.config.location", "classpath:conf/env/application.properties");
SpringApplication.run(ProviderApplication.class, args);
logger.info("start completed !");
}
}
四、编写服务消费者 rpc-demo-consume:
application.properties配置:
spring.application.name=rpc-demo-consume
server.port=8082
eureka.client.serviceUrl.defaultZone=http://192.168.10.55:8301/eureka/
eureka.instance.prefer-ip-address=true
#eureka client间隔多久去拉取服务注册信息,默认为30秒
eureka.client.registry-fetch-interval-seconds=10
#心跳间隔
eureka.instance.lease-renewal-interval-in-seconds=5
#即服务续约到期时间(缺省为90s)
eureka.lease-expiration-duration-in-seconds=10
server.contextPath=/consume
server.tomcat.max-threads=500
编写服务类:
/***
** 由于服务提供者加了context-path 所以这得在服务名后加上path或者在后面的RequestMapping加上/provider/user/**
**/
@FeignClient(name="rpc-demo-provider/provider")
public interface FeignDemoProvider {
@RequestMapping("/user/sayHello")
String sayHello();
@RequestMapping("/user/getUser")
User getUser();
}
@RestController
@RequestMapping("/consume/hello")
public class HelloCatfishController {
@Autowired
private HelloCatfishProvider helloProvider;
@RequestMapping("/sayHello")
public String sayHello() {
return helloProvider.sayHello();
}
@RequestMapping("/getUser")
public User getUser() {
return helloProvider.getUser();
}
}
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class ConsumeApplication {
private final static Logger logger = LogManager.getLogger(ConsumeApplication.class);
public static void main(String[] args) {
System.setProperty("spring.config.location", "classpath:conf/env/application.properties");
SpringApplication.run(ConsumeApplication.class, args);
logger.info("start completed !");
}
}
五、测试:
分别启动 eureka-register、 rpc-demo-provider、rpc-demo-consume 服务;
访问:localhost:/8082/consume/hello/getUser 显示如下:
{
"name": "张三",
"age": 25,
"brithday": 1530849865821
}