首页
归档
留言
广告合作
友链
美女主播
Search
1
博瑞GE车机升级/降级
5,146 阅读
2
Mac打印机设置黑白打印
4,517 阅读
3
修改elementUI中el-table树形结构图标
4,516 阅读
4
Mac客户端添加腾讯企业邮箱方法
4,351 阅读
5
intelliJ Idea 2022.2.X破解
4,060 阅读
Java
HarmonyOS Next
Web前端
微信开发
开发辅助
App开发
数据库
随笔日记
登录
/
注册
Search
标签搜索
Spring Boot
Java
Spring Cloud
Mac
mybatis
WordPress
Nacos
Spring Cloud Alibaba
Mybatis-Plus
jQuery
Java Script
asp.net
微信小程序
Sentinel
UniApp
MySQL
asp.net core
IntelliJ IDEA
Jpa
树莓派
Laughing
累计撰写
570
篇文章
累计收到
1,424
条评论
首页
栏目
Java
HarmonyOS Next
Web前端
微信开发
开发辅助
App开发
数据库
随笔日记
页面
归档
留言
广告合作
友链
美女主播
搜索到
2
篇与
的结果
2021-07-17
Spring Cloud Eureka消费者获取服务者信息
在Spring Cloud Eureka配置集群中,我们介绍了Eureka集群部署的方式,这篇文章,我们继续编写服务的提供者及服务的消费者。服务提供者我们创建microservice-simple-provider-user工程,作为服务的提供者。添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>添加配置文件spring: application: name: microservice-provider-user eureka: client: service-url: default-zone: http://peer1:8761/eureka,http://peer2:8762/eureka instance: prefer-ip-address: true metadata-map: my-metadata: 自定义元数据创建用户实体User.java@Data public class User { private Long id; private String userName; private String name; private Integer age; private BigDecimal balance; }创建服务提供者@RestController public class UserController { @GetMapping("{id}") public User findById(@PathVariable Long id){ User user = new User(); user.setId(id); user.setName("zhangsan"); user.setName("张三"); user.setAge(31); user.setBalance(new BigDecimal(300)); return user; } }服务消费者我们创建microservice-consumer-movie工程,作为服务的消费者。添加依赖添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>添加配置文件spring: application: name: microservice-provider-user eureka: client: service-url: default-zone: http://peer1:8761/eureka,http://peer2:8762/eureka instance: prefer-ip-address: true metadata-map: my-metadata: 自定义元数据我们通过metadata-map可以自定义元数据。测试获取服务提供者信息import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; /** * 博客:https://www.xiangcaowuyu.net * Description: * * @Author: 香草物语 * DateTime: 2021-07-17 22:11 */ @RestController public class MovieController { @Resource private DiscoveryClient discoveryClient; @GetMapping("/user-instance") public List<ServiceInstance> showInfo(){ return discoveryClient.getInstances("microservice-provider-user"); } }我们访问http://localhost:8081/user-instance,可以获取到服务提供者的详细信息[{ "scheme": "http", "host": "192.168.3.48", "port": 8080, "metadata": { "management.port": "8080", "my-metadata": "自定义元数据" }, "secure": false, "uri": "http://192.168.3.48:8080", "serviceId": "MICROSERVICE-PROVIDER-USER", "instanceId": "DESKTOP-PPDVS3E:microservice-provider-user", "instanceInfo": { "instanceId": "DESKTOP-PPDVS3E:microservice-provider-user", "app": "MICROSERVICE-PROVIDER-USER", "appGroupName": null, "ipAddr": "192.168.3.48", "sid": "na", "homePageUrl": "http://192.168.3.48:8080/", "statusPageUrl": "http://192.168.3.48:8080/actuator/info", "healthCheckUrl": "http://192.168.3.48:8080/actuator/health", "secureHealthCheckUrl": null, "vipAddress": "microservice-provider-user", "secureVipAddress": "microservice-provider-user", "countryId": 1, "dataCenterInfo": { "@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo", "name": "MyOwn" }, "hostName": "192.168.3.48", "status": "UP", "overriddenStatus": "UNKNOWN", "leaseInfo": { "renewalIntervalInSecs": 30, "durationInSecs": 90, "registrationTimestamp": 1626531693579, "lastRenewalTimestamp": 1626531693579, "evictionTimestamp": 0, "serviceUpTimestamp": 1626531693579 }, "isCoordinatingDiscoveryServer": false, "metadata": { "management.port": "8080", "my-metadata": "自定义元数据" }, "lastUpdatedTimestamp": 1626531693579, "lastDirtyTimestamp": 1626531693019, "actionType": "ADDED", "asgName": null } }]
2021年07月17日
1,193 阅读
0 评论
0 点赞
2021-07-17
Spring Cloud Eureka配置集群
Eureka Server可以通过运行多个实例并通过相互注册的方式实现高可用性的部署。Eureka Server实例会彼此通过增量同步的方式同步信息,确保节点数据之间的一致性。配置hosts由于我这里部署到同一个电脑上,因此需要配置hosts,以便进行服务之间的区分。win电脑hosts文件位于:C:\Windows\System32\drivers\etc\hostsMac及Linux电脑hosts文件位于:/etc/hosts在hosts文件末尾增加以下内容:C:\Windows\System32\drivers\etc配置服务提供者分别创建两个Spring Boot应用,分别为microservice-discovery-eureka、microservice-discovery-eureka-ha,代表两个服务提供者工程。添加依赖两个工程,同时添加以下依赖。<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>修改启动类两个工程的启动类,分别添加@EnableEurekaServer注解。@SpringBootApplication @EnableEurekaServer public class MicroserviceDiscoveryEurekaApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args); } }microservice-discovery-eureka配置文件如下spring: application: name: microservice-discovery-eureka server: port: 8761 eureka: instance: hostname: peer1 appname: microservice-discovery-eureka prefer-ip-address: false client: serviceUrl: defaultZone: http://peer2:8762/eureka # false表示不向注册中心注册自己 register-with-eureka: true # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 fetch-registry: truemicroservice-discovery-eureka-ha配置文件如下spring: application: name: microservice-discovery-eureka server: port: 8762 eureka: instance: hostname: peer2 appname: microservice-discovery-eureka client: serviceUrl: defaultZone: http://peer1:8761/eureka # false表示不向注册中心注册自己 register-with-eureka: true # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 fetch-registry: true可以看到,两个工程通过相互注册的方式,实现集群。配置文件,需要注意以下内容[alt type="success"]hostname需要不同[/alt][alt type="info"]appname必须保持一致[/alt][alt type="warning"]defaultZone需要使用我们配置的IP映射,而不是使用IP[/alt]
2021年07月17日
963 阅读
0 评论
1 点赞