2_网站建设的一般步骤包含哪些?,网站建设时间怎么查询,软件工程文档,seo优化方案模板下面写一个简单的demo验证下eureka#xff0c;实现服务注册、服务发现。
一、单节点#xff1a;
1、api#xff1a; 封装其他组件需要共用的dto
2、eureka-service服务注册中心#xff1a; #xff08;1#xff09;pom:
?xml version1.0 encoding实现服务注册、服务发现。
一、单节点
1、api 封装其他组件需要共用的dto
2、eureka-service服务注册中心 1pom:
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.demo.cloud/groupIdartifactIdmyspringcloud-eureka-server/artifactIdversion1.0-SNAPSHOT/version!-- springBoot --parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.5.9.RELEASE/version/parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingjava.version1.8/java.versionspring-cloud.versionEdgware.RELEASE/spring-cloud.version/propertiesdependencies!--eureka-server--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-eureka-server/artifactId/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring-cloud.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementrepositoriesrepositoryidspring-milestones/idnameSpring Milestones/nameurlhttps://repo.spring.io/libs-milestone/urlsnapshotsenabledfalse/enabled/snapshots/repository/repositories/project 2application.properties:在默认设置下该服务注册中心也会将自己作为客户端来尝试注册它自己所以需要禁用它的客户端注册行为
server.port1111
eureka.client.registerWithEurekafalse
eureka.client.fetchRegistryfalse
eureka.client.serviceUrl.defaultZonehttp://localhost:1111/eureka/
3启动类
package com.demo.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;SpringBootApplication
EnableEurekaServer
public class MyEurekaServerApplication {public static void main(String args[]){SpringApplication.run(MyEurekaServerApplication.class,args);}
}启动后访问http://localhost:1111/ 就可以从后台监控服务了是不是比dubbo搭建zk注册中心方便多了 此时还没有服务注册过来可以看到application下是空的。
3、eureka-client注册服务提供者将原有的springboot工程改造注册到eureka注册中心 1pom:
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.demo.cloud/groupIdartifactIdmyspringcloud-eureka-client/artifactIdversion1.0-SNAPSHOT/version!-- springBoot --parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.5.9.RELEASE/version/parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingjava.version1.8/java.versionspring-cloud.versionEdgware.RELEASE/spring-cloud.version/propertiesdependencies!--eureka-server--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-eureka-server/artifactId/dependencydependencygroupIdcom.demo.cloud.api/groupIdartifactIdmyspringcloud-api/artifactIdversion1.0-SNAPSHOT/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- mybatis --dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion1.1.1/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.21/version/dependencydependencygroupIdcommons-collections/groupIdartifactIdcommons-collections/artifactIdversion3.2.1/version/dependencydependencygroupIdcommons-lang/groupIdartifactIdcommons-lang/artifactIdversion2.5/version/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring-cloud.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementrepositoriesrepositoryidspring-milestones/idnameSpring Milestones/nameurlhttps://repo.spring.io/libs-milestone/urlsnapshotsenabledfalse/enabled/snapshots/repository/repositories/project
2application.properties:
server.port2222
server.context-path/myService
spring.application.namemy-service
eureka.client.serviceUrl.defaultZonehttp://localhost:1111/eureka/#mybatis
spring.datasource.driver-class-namecom.mysql.jdbc.Driver
spring.datasource.urljdbc:mysql://localhost:3306/demo?useUnicodetruecharacterEncodingutf-8useSSLfalse
spring.datasource.usernameroot
spring.datasource.passwordwtyy
mybatis.mapper-locationsclasspath*:Mapper/*Mapper.xml3dao、service、Mappermybatis持久化部分此处省略
4controller
package com.demo.cloud.controller;import com.demo.cloud.dto.AuthorityDTO;
import com.demo.cloud.dto.UserDTO;
import com.demo.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;RequestMapping(/user)
RestController
public class UserApiController {Autowiredprivate UserService userService;Value(${server.port})private String port;RequestMapping(/findByUserName)public UserDTO findByUserName(String username){return userService.findByUserName(username);}RequestMapping(/getAllUsers)public ListUserDTO getAllUsers(){System.out.println(port);return userService.getAllUsers();}RequestMapping(/getAuthortiesByUserId)public ListAuthorityDTO getAuthortiesByUserId(Integer userId){return userService.getAuthortiesByUserId(userId);}RequestMapping(/addUser)public void addUser1(RequestBody UserDTO userDTO){userService.addUser(userDTO);}}5启动类
package com.demo.cloud;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;SpringBootApplication
EnableEurekaClient
MapperScan(com.demo.cloud.dao)
public class MyEurekaClientApplication {public static void main(String args[]){SpringApplication.run(MyEurekaClientApplication.class,args);}
}注意这里使用的是EnableEurekaClient注解也可以使用EnableDiscoveryClient注解
EnableEurekaClient是Eureka特有的注解用于启动Eureka客户端。当使用Eureka作为注册中心时就推荐使用EnableEurekaClient注解应用启动后会自动注册到Eureka Server并完成服务治理。
EnableDiscoveryClient是Spring Cloud通用的注解可以与Eureka、Consul等多种注册中心对接。当我们的微服务同时需要与多个注册中心集成时就需要使用EnableDiscoveryClient注解。
可以说EnableEurekaClient是EnableDiscoveryClient的一个具体实现如果项目中注册中心只使用Eureka那么使用EnableEurekaClient更加方便和简单。但如果要切换到其他的注册中心改动较大。启动项目再浏览下http://localhost:1111/ 可以看到注册成功了。
二、高可用部署
1、先部署eukera-service的高可用
2、再部署eureka-client的高可用连接注册中心的配置项改为多个,以逗号分隔然后将该组件部署多个节点即可
eureka.client.serviceUrl.defaultZonehttp://localhost:1111/eureka/,http://xxx.xx.xxx:1111/eureka/