Springcloud框架搭建

来源:互联网 发布:关于套路的网络用语 编辑:程序博客网 时间:2024/06/05 17:50

//首先创建注册方的配置
//第一点,添加pop.xml依赖

        <dependency>    <groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId>        </dependency>    </dependencies>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>Dalston.SR1</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>//第二步,在application.properties配置服务注册中心spring.application.name=server-register-centerserver.port=1111eureka.instance.hostname=localhosteureka.client.register-with-eureka=falseeureka.client.fetch-registry=false//第三步,在DemoApplication中添加注解@EnableEurekaServer

//在服务方的配置
//第一点,添加pop.xml依赖

        <!--服务提供方的依赖包-->        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId>        </dependency><dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>Brixton.RELEASE</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>

//第二步,在application.properties配置

#spring cloud配置spring.application.name=simple-service-providerserver.port=2222eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

//第三步,在DemoApplication中添加注解

@EnableDiscoveryClient

//在controller中的简约配置

        @RequestMapping("/plus/{a}/{b}")        public Integer plus(@PathVariable Integer a, @PathVariable Integer b){        return a+b;        }

//配置消费方的配置
//也是先添加pop.xml

<dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-feign</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-eureka</artifactId>        </dependency>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>Brixton.RELEASE</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>

//第二步,在application.properties配置

spring.application.name=simple-service-consumerserver.port=3333eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

//第三步,在DemoApplication中添加注解

//注解在DemoApplication类上@EnableDiscoveryClient//在类里面    @Bean    public RestTemplate restTemplate() {        return new RestTemplate();    }

//消费方controller

    @Autowired    private LoadBalancerClient lbc;    @Autowired    private RestTemplate template;    @RequestMapping("/comsumer/{a}/{b}")    public String computePlus(@PathVariable Integer a, @PathVariable Integer b){        //发现服务        ServiceInstance choose = lbc.choose("simple-service-provider");        String url = "http://" + choose.getHost() + ":" + choose.getPort() + "/plus/"+a+"/"+b;        Integer result = template.getForObject(url, Integer.class);        return a+"+"+b+"="+result;    }

省下的就是对各个服务的添加其他的操作

原创粉丝点击